﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Deployment;
using System.Deployment.Application;
using System.IO;
using control.toolbar;
using CMS_GYM.Interface;


namespace CMS_GYM
{
    public partial class frmMain : Form
    {
        #region Attribute

        ToolBarManager _toolBarManager;
        private StatusBarPanel sbPnlVungTrong = new StatusBarPanel();
        private StatusBarPanel sbPnlHoTen = new StatusBarPanel();
        private StatusBarPanel sbPnlTenDangNhap = new StatusBarPanel();
        private StatusBarPanel sbPnlNgay = new StatusBarPanel();
        private StatusBarPanel sbPnlGio = new StatusBarPanel();
        Timer BoDinhGio;
        Timer BoDinhGio_Backup;
        StatusBar statusBar;
        private bool _Trial;
        #endregion

        private void initToolBarManager()
        {
            BoDinhGio = new Timer();
            BoDinhGio.Interval = 30000;//30 second
            BoDinhGio.Enabled = true;
            BoDinhGio.Tick += new EventHandler(BoDinhGio_Tick);

            _toolBarManager = new ToolBarManager(this, this);
            //panelTop.AutoSize = true;
            //panelTop.Size = new Size(this.Width, 44);
            //_toolBarManager.AddControl(panelTop, DockStyle.Top);
            statusBar = new StatusBar();
            statusBar.ShowPanels = true;
            statusBar.Panels.AddRange((StatusBarPanel[])new StatusBarPanel[] { sbPnlVungTrong, sbPnlHoTen, sbPnlTenDangNhap, sbPnlNgay, sbPnlGio });

            sbPnlVungTrong.BorderStyle = StatusBarPanelBorderStyle.Raised;
            sbPnlVungTrong.AutoSize = StatusBarPanelAutoSize.Spring;
            //sbPnlHoTen.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

            sbPnlHoTen.BorderStyle = StatusBarPanelBorderStyle.Raised;
            //sbPnlHoTen.AutoSize = StatusBarPanelAutoSize.Spring;
            //sbPnlHoTen.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

            sbPnlTenDangNhap.BorderStyle = StatusBarPanelBorderStyle.Raised;
            //sbPnlTenDangNhap.AutoSize = StatusBarPanelAutoSize.Spring;
            // sbPnlHoTen.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

            sbPnlNgay.BorderStyle = StatusBarPanelBorderStyle.Raised;
            //sbPnlNgay.AutoSize = StatusBarPanelAutoSize.Spring;
            // sbPnlNgay.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

            sbPnlGio.BorderStyle = StatusBarPanelBorderStyle.Raised;
            //sbPnlGio.AutoSize = StatusBarPanelAutoSize.Spring;
            // sbPnlGio.Alignment = System.Windows.Forms.HorizontalAlignment.Right;

            this.Controls.Add(statusBar);
        }

        private void BoDinhGio_Tick(object sender, EventArgs e)
        {
            try
            {
                if (DateTime.Now.Hour == 0 && DateTime.Now.Minute == 0)
                {
                    sbPnlNgay.Text = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
                }
                DateTime Gio = DateTime.Now;
                sbPnlGio.Text = Gio.ToShortTimeString();
            }
            catch (Exception Loi)
            {

            }
        }

        private void BoDinhGio_Backup_Tick(object sender, EventArgs e)
        {
            try
            {
                CheckAutoBackup();

            }
            catch (Exception Loi)
            {

            }
        }

        void AutoBackup(bool _dayofweek)
        {
            if (_dayofweek == true)
            {
                if (DateTime.Now.Hour == PublicInformatin.SaoLuu.ThoiGian.Hour && DateTime.Now.Minute == PublicInformatin.SaoLuu.ThoiGian.Minute)
                {
                    String TenTapTin = "Backup_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + ".bak";
                    if (new clsSaoLuuBUS().SaoLuuDuLieu(PublicInformatin.SaoLuu.DuongDan + @"\" + TenTapTin, "BMBez2010") != -1)
                    {
                        MessageBox.Show("Sao lưu dữ liệu thành công với tên tâp tin : !" + TenTapTin, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Sao lưu dữ liệu thất bại! Xin vui lòng kiểm tra lại đường đẫn sao lưu dự phòng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        void CheckAutoBackup()
        {
            switch (DateTime.Now.DayOfWeek.ToString())
            {
                case "Monday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu2);
                    break;
                case "Tuesday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu3);
                    break;
                case "Wednesday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu4);
                    break;
                case "Thursday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu5);
                    break;
                case "Friday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu5);
                    break;
                case "Saturday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu7);
                    break;
                case "Sunday":
                    AutoBackup(PublicInformatin.SaoLuu.Thu1);
                    break;
            }
        }

        #region CountNumberOfWeekDayInDateRange(DayOfWeek day, DateTime start, DateTime end)
        public int CountNumberOfWeekDayInDateRange(DayOfWeek day, DateTime start, DateTime end)
        {
            TimeSpan ts = end - start;                       // Total duration
            int count = (int)Math.Floor(ts.TotalDays / 7);   // Number of whole weeks
            int remainder = (int)(ts.TotalDays % 7);         // Number of remaining days
            int sinceLastDay = (int)(end.DayOfWeek - day);   // Number of days since last [day]
            if (sinceLastDay < 0) sinceLastDay += 7;         // Adjust for negative days since last [day]
            // If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
            if (remainder >= sinceLastDay) count++;
            return count;
        }
        #endregion

        public frmMain()
        {
            InitializeComponent();
        }


        public frmMain(bool IsTrial)
        {          
            InitializeComponent();
            //initToolBarManager();
            
            if (IsTrial == false)
            {
                //Đã đăng ký 
            }
            _Trial = IsTrial;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                if (clsConnection.IsCheckConnect(clsConnection.ServerName, clsConnection.Databasename, clsConnection.LoginName, clsConnection.LoginPassword) == false)
                {
                    clsConnection.ReadKeys();
                }
                if ((string.IsNullOrEmpty(clsConnection.ServerName ) || string.IsNullOrEmpty(clsConnection.Databasename) || string.IsNullOrEmpty(clsConnection.LoginName) || string.IsNullOrEmpty(clsConnection.LoginPassword)))
                {
                    frmLoginServer F = new frmLoginServer();
                    F.ShowDialog();
                    if (frmLoginServer.isConnect == false)
                    {
                        Application.Exit();
                    }
                    else
                    {
                        KiemTraHanDung();
                        DangNhapHeThong();
                        ShowManHinhChinh();
                    }
                }
                else
                {
                    if (clsConnection.IsCheckConnect(clsConnection.ServerName, clsConnection.Databasename, clsConnection.LoginName, clsConnection.LoginPassword) == false)
                    {
                        frmLoginServer F = new frmLoginServer();
                        F.ShowDialog();
                        if (frmLoginServer.isConnect == false)
                        {
                            Application.Exit();
                        }
                        else
                        {
                            KiemTraHanDung();
                            DangNhapHeThong();
                            ShowManHinhChinh();
                        }
                    }
                    else
                    {
                        KiemTraHanDung();
                        DangNhapHeThong();
                        ShowManHinhChinh();
                    }
                }
            }
            catch (Exception Loi)
            {
                MessageBox.Show("Lỗi kết nối cơ sở dữ liệu", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ShowManHinhChinh()
        {
            if (clsUser.LayThongTinPhanQuyen("ThongKeDoanhThuChiPhi").ChucNang.Them)
            {
                ShowDashBoard();
            }
            else
            {
                ShowXuatPhieuThu();
            }
        }

        private void KiemTraHanDung()
        {
            //String ThongBao = "";
            //clsCongTyBUS CongTyBus = new clsCongTyBUS();
            //DateTime NgaySuDung = CongTyBus.LayNgaySuDung();
            //Boolean HanDung = CongTyBus.LayHanDung();
            //TimeSpan ts = DateTime.Now - NgaySuDung;
            //int SoNgaySuDung = ts.Days;
            //if (HanDung == true)
            //{
            //    if (NgaySuDung.Day == 1 && NgaySuDung.Month == 1 && NgaySuDung.Year == 1111)
            //    {
            //        CongTyBus.CapNhatNgaySuDung(DateTime.Now.ToShortDateString());
            //    }
            //    else
            //    {
            //        if (SoNgaySuDung <= 365)//365*3= 3 nam
            //        {
            //            if (SoNgaySuDung >= (358) && SoNgaySuDung <= 365)
            //            {
            //                ThongBao = "Hiện nay sản phẩm của bạn chỉ còn lại " + SoNgaySuDung.ToString() + " ngày sử dụng sản phẩm, xin vui lòng liên hệ với chúng tôi để bạn có thể sử dụng tiếp tục sản phẩm EZ Sales.";
            //                MessageBox.Show(ThongBao, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //            }
            //        }
            //        else
            //        {
            //            CongTyBus.CapNhatHanDung("false");
            //            ThongBao = "Hiện nay sản phẩm bạn đang sử dụng đã hết hạn,xin vui lòng liên hệ với chúng tôi để bạn có thể sử dụng tiếp tục sản phẩm EZ Sales.";
            //            MessageBox.Show(ThongBao, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //            Application.Exit();

            //        }
            //    }
            //}
            //else
            //{
            //    ThongBao = "Hiện nay sản phẩm bạn đang sử dụng đã hết hạn,xin vui lòng liên hệ với chúng tôi để bạn có thể sử dụng tiếp tục sản phẩm EZ Sales.";
            //    MessageBox.Show(ThongBao, "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    Application.Exit();
            //}
        }

        private void DangNhapHeThong()
        {
            frmLogin F = new frmLogin();
            F.ShowDialog();
            if (frmLogin.isLogin == true)
            {
                //treeViewChucNang.SelectedNode = treeViewChucNang.Nodes[0];
                //treeViewChucNang.SelectedNode.Expand();
                //// hạ cờ các chức năng trước của người dùng
                //ClearAllTooltipTreeNode();
                //this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
                ////Lấy thông tin người dùng và chức năng phân quyền của người đó
                clsNhanVienDTO NguoiDung = frmLogin.User.LayThongTinNguoiDung();
                labelTenDangNhap.Text ="Tên đăng nhập: "+ NguoiDung.TenNguoiDung;
                labelNguoiDung.Text = "Người dùng: " + NguoiDung.TenNhanVien;
                labelLoginTime.Text = "Thời gian đăng nhập: " + DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString() + ' ' + DateTime.Now.Hour.ToString() + ':' + DateTime.Now.Minute.ToString();
                SetDisableMenu();
                PhanQuyenMenu(clsUser.ChucNangPhanQuyen);
                mnLogout.Enabled = true;
                mnThoat.Enabled = true;
                ////Xóa các chức năng người dùng này không có quyền hạn
                //PhanQuyenChucNang();
                ////Xóa đi các nút cha trống
                //RemoveNodeEmpty();
                //sbPnlHoTen.Text = NguoiDung.TenNhanVien;
                //sbPnlTenDangNhap.Text = NguoiDung.TenNguoiDung;
                //DateTime Gio = DateTime.Now;
                //sbPnlGio.Text = Gio.ToShortTimeString();
                //sbPnlNgay.Text = DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
                //PublicInformatin.KhoiTao();
                //panelTop.Controls["lbTenCongTy"].Text = PublicInformatin.CongTy.TenCongTy;
                //BoDinhGio_Backup = new Timer();
                //BoDinhGio_Backup.Interval = 60000;//1 minitures
                //BoDinhGio_Backup.Enabled = true;
                //BoDinhGio_Backup.Tick += new EventHandler(BoDinhGio_Backup_Tick);
            }
            else
            {
                Application.Exit();
            }
        }

        public static void SetDisableMenuBar(DevComponents.DotNetBar.SubItemsCollection menuItems, bool isShow)
        {
            try
            {
                foreach (DevComponents.DotNetBar.BaseItem mnItem in menuItems)
                {
                    if (mnItem.SubItems.Count == 0) mnItem.Enabled = false;
                    SetDisableMenuBar(mnItem.SubItems, isShow);//Duyệt đệ quy menu items
                }
            }
            catch (Exception ex) { }
        }

        public static void SetDesableMenuToolStrip(ToolStripItemCollection menuItems, bool isShow)
        {
            try
            {
                foreach (ToolStripItem mnItem in menuItems)
                {
                    if (mnItem.GetType() != typeof(ToolStripDropDownButton))
                    {
                        mnItem.Enabled = false;
                    }
                    else
                    {
                        ToolStripDropDownButton drdButtols = (ToolStripDropDownButton)mnItem;
                        SetDesableMenuToolStrip(drdButtols.DropDownItems, isShow);//Duyệt đệ quy menu items
                    }
                }
            }
            catch (Exception ex) { }
        }

        public static void ShowMenuToolStrip(ToolStripItemCollection menuItems, string menuId, bool isShow)
        {
            try
            {
                foreach (ToolStripItem mnItem in menuItems)
                {
                    if (mnItem.Name.ToLower() == menuId.ToLower())
                    {
                        mnItem.Enabled = isShow; return;
                    }
                    //else if (mnItem.GetType() != typeof(ToolStripDropDownButton))
                    //{
                    //    mnItem.Enabled = false;
                    //}
                    else if (mnItem.GetType() == typeof(ToolStripDropDownButton))
                    {
                        ToolStripDropDownButton drdButtols = (ToolStripDropDownButton)mnItem;
                        ShowMenuToolStrip(drdButtols.DropDownItems, menuId, isShow);//Duyệt đệ quy menu items
                    }
                }
            }
            catch (Exception ex) { }
        }

        public static void ShowMenuBar(DevComponents.DotNetBar.SubItemsCollection menuItems, string menuId, bool isShow)
        {
            try
            {
                foreach (DevComponents.DotNetBar.BaseItem mnItem in menuItems)
                {
                    if (mnItem.Name.ToLower() == menuId.ToLower())
                    {
                        mnItem.Enabled = isShow; return;
                    }
                    //else if (mnItem.SubItems.Count == 0)
                    //{
                    //    mnItem.Enabled = false;
                    //}

                    ShowMenuBar(mnItem.SubItems, menuId, isShow);//Duyệt đệ quy menu items
                }
            }
            catch (Exception ex) { }
        }

        private void SetDisableMenu()
        {
            try
            {
               SetDisableMenuBar(barMenuMain.Items, false);
               SetDesableMenuToolStrip(toolStripTheHinh.Items, false);
               SetDesableMenuToolStrip(toolStripCauLong.Items, false);
            }
            catch (Exception ex) { }
        }

        private void PhanQuyenMenu(List<clsPhanQuyenChucNangDTO> PhanQuyen)
        {
            try
            {

                foreach (clsPhanQuyenChucNangDTO QH in PhanQuyen)
                {
                    //Phân quyên menu Bar main 
                    ShowMenuBar(barMenuMain.Items, "mn" + QH.ChucNang.TenMenu, true);

                    //Phân quyên menu ToolStrip THC 
                    ShowMenuToolStrip(toolStripTheHinh.Items, "btn" + QH.ChucNang.TenMenu, true);
                    //Phân quyên menu ToolStrip KTS 
                    ShowMenuToolStrip(toolStripCauLong.Items, "btn" + QH.ChucNang.TenMenu, true);
                    //Phân quyên menu ToolStrip Internet
                    //ShowMenuToolStrip(toolStripINT.Items, "btn" + ett.Q_MENU_ID, true);
                }
            }
            catch (Exception ex) { }
        }


        private void ClearAllTooltipTreeNode()
        {
            //for (int i = 0; i < treeViewChucNang.Nodes[0].Nodes.Count; i++)
            //{
            //    for (int j = 0; j < treeViewChucNang.Nodes[0].Nodes[i].Nodes.Count; j++)
            //    {
            //        //if (treeViewChucNang.Nodes[j].Text.Trim() == "Bán Hàng" || treeViewChucNang.Nodes[j].Text.Trim() == "Mua Hàng" ||
            //        //    treeViewChucNang.Nodes[j].Text.Trim() == "Quản Lý Kho" || treeViewChucNang.Nodes[j].Text.Trim() == "Quản Lý Tiền" ||
            //        //    treeViewChucNang.Nodes[j].Text.Trim() == "Thiết Lập" || treeViewChucNang.Nodes[j].Text.Trim() == "Quản lý bán hàng")
            //        //{
            //        //    continue;
            //        //}
            //        //else
            //        //{
            //        treeViewChucNang.Nodes[0].Nodes[i].Nodes[j].ToolTipText = "";
            //        //}
            //    }
            //}
        }

        private void PhanQuyenChucNang()
        {
            //for (int i = 0; i < treeViewChucNang.Nodes[0].Nodes.Count; i++)
            //{
            //    for (int j = 0; j < treeViewChucNang.Nodes[0].Nodes[i].Nodes.Count; j++)
            //    {
            //        if (treeViewChucNang.Nodes[0].Nodes[i].Nodes[j].ToolTipText.Trim() == "")
            //        {
            //            treeViewChucNang.Nodes[0].Nodes[i].Nodes[j].Remove();
            //            i--;
            //            break;
            //        }
            //    }
            //}
        }

        private void RemoveNodeEmpty()
        {
            //for (int i = 0; i < treeViewChucNang.Nodes[0].Nodes.Count; i++)
            //{
            //    if (treeViewChucNang.Nodes[0].Nodes[i].Nodes.Count == 0)
            //    {
            //        treeViewChucNang.Nodes[0].Nodes[i].Remove();
            //        i = 0;
            //    }
            //}
        }

        //public Form getForm(string formName)
        //{
        //    bool Result = false;
        //    int i = 0;
        //    //Đóng tất cả các form còn lại
        //    int j = 0;
        //    while (j < this.MdiChildren.Length)
        //    {
        //        this.MdiChildren[j].Close();
        //        j++;
        //    }
        //    //Mở form hiện hành đang chọn
        //    while (i < this.MdiChildren.Length && Result == false)
        //    {
        //        if (this.MdiChildren[i].Name == formName)
        //            Result = true;
        //        else i++;
        //    }
        //    if (Result) return this.MdiChildren[i];
        //    else return (Form)null;
        //}

        public Form getForm(string formName)
        {
            bool Result = false;
            int i = 0;
            while (i < this.MdiChildren.Length && Result == false)
            {
                if (this.MdiChildren[i].Name == formName)
                    Result = true;
                else i++;
            }
            if (Result) return this.MdiChildren[i];
            else return (Form)null;
        }

        private void treeViewChucNang_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ////==START:  Minh edit 06/09/2009 -> Bo Phan Quyen Chuc Nang nay cho mat hang __________________________________________________________
            try
            {
            //    if (treeViewChucNang.SelectedNode.Tag != null)
            //    {
            //        string tag = treeViewChucNang.SelectedNode.Tag.ToString();
            //        switch (tag)
            //        {
            //            #region minhnb
            //            //==START:  Minh edit 08/09/2009 -> Ban Hang __________________________________________________________
            //            case "TagBanLe":
            //                showFormBanLe();
            //                break;
            //            case "TagBanSi":
            //                showFormBanSi();
            //                break;
            //            case "TagThuTien":
            //                showFormThuTien();
            //                break;
            //            case "TagKhachHang":
            //                showFormKhachHang();
            //                break;
            //            case "TagMatHangBan":
            //                showFormMatHangBan();
            //                break;
            //            case "TagXemDonHangDaBan":
            //                showFormXemDonHangDaBan();
            //                break;
            //            case "TagDanhSachPhieuThu":
            //                showFormDSPhieuThu();
            //                break;
            //            case "TagDoanhThuTheoNVBH":
            //                showFormDoanhThuTheoNVBH();
            //                break;
            //            case "TagCongNoKhachHang":
            //                showFormCongNoKhachHang();
            //                break;
            //            case "TagDoanhThu_ChiPhi":
            //                showFormDoanhThu_ChiPhi();
            //                break;
            //            case "TagSanPhamBanChay":
            //                showFormSanPhamBanChay();
            //                break;
            //            case "TagNhapHangVaoKho":
            //                showFormNhapHangVaoKho();
            //                break;
            //            case "TagTraTien":
            //                showFormTraTien();
            //                break;
            //            case "TagNhaCungCap":
            //                showFormNhaCungCap();
            //                break;
            //            case "TagMatHangMua":
            //                showFormMatHangMua();
            //                break;
            //            case "TagDonHangDaMua":
            //                showFormDonHangDaMua();
            //                break;
            //            case "TagDanhSachPhieuChi":
            //                showFormDanhSachPhieuChi();
            //                break;
            //            case "TagDonDatHang":
            //                showFormDonDatHang();
            //                break;
            //            case "TagCongNoNhaCungCap":
            //                showFormCongNoNhaCungCap();
            //                break;
            //            case "TagThietLapDinhMucHang":
            //                showFormThietLapDinhMuc();
            //                break;
            //            case "TagChiTietHangNhap":
            //                showFormChiTietHangNhap();
            //                break;
            //            case "TagChiTietHangXuat":
            //                showFormChiTietHangXuat();
            //                break;
            //            case "TagTonKhoHangHoa":
            //                showFormTonKhoHangHoa();
            //                break;
            //            case "TagQuanLyDonHangDaBan":
            //                showFormQuanLyDonHangDaBan();
            //                break;
            //            case "TagTienQuyDauKy":
            //                showTienQuyDauKy();
            //                break;
            //            case "TagThuTienKhac":
            //                showThuTienKhac();
            //                break;
            //            case "TagTraTienKhac":
            //                showTraTienKhac();
            //                break;
            //            case "TagQuyTienMat":
            //                showQuyTienMat();
            //                break;
            //            case "TagNhanVien":
            //                showNhanVien();
            //                break;
            //            case "TagNhomHang":
            //                showNhomHang();
            //                break;
            //            case "TagThongTinCongTy":
            //                showThongTinCongTy();
            //                //panelTop.Controls["lbTenCongTy"].Text = PublicInformatin.CongTy.TenCongTy;
            //                break;
            //            case "TagPhanQuyen":
            //                showPhanQuyen();
            //                break;
            //            case "TagSaoLuuDuPhong":
            //                showSaoLuuDuPhong();
            //                break;
            //            case "TagKhoiPhucDuLieu":
            //                showPhucHoiDuLieu();
            //                break;
            //            case "TagKetNoiCSDL":
            //                showKetNoiCSDL();
            //                break;
            //            //==END:  Minh edit 08/09/2009 -> Ban Hang __________________________________________________________
            //            #endregion

            //        }
            //    }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
            //==END:  Minh edit 06/09/2009 -> Bo Phan Quyen Chuc Nang nay cho mat hang __________________________________________________________
        }

        #region minhnb
        //Ban hang
       
        public frmThongTinCongTy showThongTinCongTy()
        {
            frmThongTinCongTy F = new frmThongTinCongTy();
            F.ShowDialog();
            F.Activate();
            return null;
        }
       
     
        public frmLoginServer showKetNoiCSDL()
        {
            frmLoginServer F = new frmLoginServer();
            F.ShowDialog();
            F.Activate();
            return null;
        }

        #endregion

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.MdiChildren.Length == 0) e.Cancel = false;
            else
                if (this.MdiChildren.Length == 1 && this.MdiChildren[0].Visible == true)
                    e.Cancel = false;

                else
                {
                    int i = 0;
                    while (i < this.MdiChildren.Length)
                    {
                        if (this.MdiChildren[i].Visible == true || this.MdiChildren[i].WindowState != FormWindowState.Minimized)
                        {
                            if (this.MdiChildren[i].Name != "frmFlash")
                            {
                                this.MdiChildren[i].Visible = false;
                                this.MdiChildren[i].WindowState = FormWindowState.Minimized;
                            }
                        }
                        i++;
                    }
                }
        }


        private void btnTHCThuTien_Click(object sender, EventArgs e)
        {
            ShowXuatPhieuThu();
        }

        private void btnXuatPhieuChi_Click(object sender, EventArgs e)
        {
            ShowXuatPhieuChi();
        }

        private void btnBaoCaoBanHangHangNgay_Click(object sender, EventArgs e)
        {
            ShowBaoCaoBanHangHangNgay();
        }

        private void btnQLSCDatLichSanCau_Click(object sender, EventArgs e)
        {
            frmDatLichSanCauMoi frm = (frmDatLichSanCauMoi)getForm("frmDatLichSanCauMoi");
            if (frm == null)
            {
                frm = new frmDatLichSanCauMoi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void btnThongKeNhanh_Click(object sender, EventArgs e)
        {
            ShowDashBoard();
            
        }
        

        private void ShowXuatPhieuThu()
        {
            frmXuatPhieuThu frm = (frmXuatPhieuThu)getForm("frmXuatPhieuThu");
            if (frm == null)
            {
                frm = new frmXuatPhieuThu();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void ShowXuatPhieuChi()
        {
            frmXuatPhieuChi frm = (frmXuatPhieuChi)getForm("frmXuatPhieuChi");
            if (frm == null)
            {
                frm = new frmXuatPhieuChi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void ShowBaoCaoBanHang()
        {
            frmBaoCaoBanHang frm = (frmBaoCaoBanHang)getForm("frmBaoCaoBanHang");
            if (frm == null)
            {
                frm = new frmBaoCaoBanHang();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void ShowBaoCaoBanHangHangNgay()
        {
            frmBaoCaoBanHangHangNgay frm = (frmBaoCaoBanHangHangNgay)getForm("frmBaoCaoBanHangHangNgay");
            if (frm == null)
            {
                frm = new frmBaoCaoBanHangHangNgay();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void mnHeThong_Click(object sender, EventArgs e)
        {

        }

        private void mnDanhMuc_Click(object sender, EventArgs e)
        {

        }

        private void mnSanCau_Click(object sender, EventArgs e)
        {
            frmDanhMucSanCau frm = (frmDanhMucSanCau)getForm("frmDanhMucSanCau");
            if (frm == null)
            {
                frm = new frmDanhMucSanCau();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            frmQuanLyNguoiDung frm = (frmQuanLyNguoiDung)getForm("frmQuanLyNguoiDung");
            if (frm == null)
            {
                frm = new frmQuanLyNguoiDung();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnChucNang_Click(object sender, EventArgs e)
        {
            frmChucNangChuongTrinh frm = (frmChucNangChuongTrinh)getForm("frmChucNangChuongTrinh");
            if (frm == null)
            {
                frm = new frmChucNangChuongTrinh();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnPhanQuyenNguoiDung_Click(object sender, EventArgs e)
        {
            frmPhanQuyenNguoiDung frm = (frmPhanQuyenNguoiDung)getForm("frmPhanQuyenNguoiDung");
            if (frm == null)
            {
                frm = new frmPhanQuyenNguoiDung();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnNgayTrongTuan_Click(object sender, EventArgs e)
        {
            frmDanhMucNgayTrongTuan frm = (frmDanhMucNgayTrongTuan)getForm("frmDanhMucNgayTrongTuan");
            if (frm == null)
            {
                frm = new frmDanhMucNgayTrongTuan();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnBangGia_Click(object sender, EventArgs e)
        {
            
        }

        private void mnKhuyenMai_Click(object sender, EventArgs e)
        {
            frmKhuyenMaiMoi frm = (frmKhuyenMaiMoi)getForm("frmKhuyenMaiMoi");
            if (frm == null)
            {
                frm = new frmKhuyenMaiMoi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnChietKhau_Click(object sender, EventArgs e)
        {
            frmKhuyenMaiChietKhau frm = (frmKhuyenMaiChietKhau)getForm("frmKhuyenMaiChietKhau");
            if (frm == null)
            {
                frm = new frmKhuyenMaiChietKhau();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnBangGiaTheHinh_Click(object sender, EventArgs e)
        {
            frmBangGiaTheHinh frm = (frmBangGiaTheHinh)getForm("frmBangGiaTheHinh");
            if (frm == null)
            {
                frm = new frmBangGiaTheHinh();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnLoaiKhachHang_Click(object sender, EventArgs e)
        {
            frmLoaiKhachHangMoi frm = (frmLoaiKhachHangMoi)getForm("frmLoaiKhachHangMoi");
            if (frm == null)
            {
                frm = new frmLoaiKhachHangMoi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnLichSanCau_Click(object sender, EventArgs e)
        {
            frmDatLichSanCauMoi frm = (frmDatLichSanCauMoi)getForm("frmDatLichSanCauMoi");
            if (frm == null)
            {
                frm = new frmDatLichSanCauMoi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnCauHinhServer_Click(object sender, EventArgs e)
        {
            frmConfigServer frm = (frmConfigServer)getForm("frmConfigServer");
            if (frm == null)
            {
                frm = new frmConfigServer();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Normal;
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnALLLapHopDong_Click(object sender, EventArgs e)
        {
            frmQuanLyKhachHangMoi frm = (frmQuanLyKhachHangMoi)getForm("frmQuanLyKhachHangMoi");
            if (frm == null)
            {
                frm = new frmQuanLyKhachHangMoi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void ShowDashBoard()
        {

            frmThongKeDoanhThuChiPhi frm = (frmThongKeDoanhThuChiPhi)getForm("frmThongKeDoanhThuChiPhi");
            if (frm == null)
            {
                frm = new frmThongKeDoanhThuChiPhi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnBangGiaCauLong_Click(object sender, EventArgs e)
        {
            frmBangGiaCauLong frm = (frmBangGiaCauLong)getForm("frmBangGiaCauLong");
            if (frm == null)
            {
                frm = new frmBangGiaCauLong();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnALLDanhMucDinhKhoan_Click(object sender, EventArgs e)
        {
            frmDanhMucDinhKhoan frm = (frmDanhMucDinhKhoan)getForm("frmDanhMucDinhKhoan");
            if (frm == null)
            {
                frm = new frmDanhMucDinhKhoan();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnBaoCao_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Chức năng đang xây dựng","Thông báo");
        }

        private void mnPhanQuyenChucNang_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Chức năng đang xây dựng", "Thông báo");
        }

        private void mnNhacNoCauLong_Click(object sender, EventArgs e)
        {
            try
            {
                using (frmChonNgayThongBaoNhacNo dialog = new frmChonNgayThongBaoNhacNo())
                {
                    DialogResult result = dialog.ShowDialog();
                    int CurrentYear = DateTime.Now.Year;
                    int CurrentMonth = DateTime.Now.Month;
                    int CurrentDay = DateTime.Now.Day;
                    if (result == System.Windows.Forms.DialogResult.Cancel) return;
                    CurrentMonth = dialog.Thang;
                    CurrentYear = dialog.Nam;

                    #region Tam thoi dong lai
                    //clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
                    //DataTable dtTemp = LichChiTietBUS.LayBangThongBaoTienSanHangThang();
                    //#region xac dinh ngay cuoi thang
                    //int EndDayOfMonth = 0;
                    //switch (CurrentMonth)
                    //{
                    //    case 1:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 2:
                    //        EndDayOfMonth = 28;
                    //        break;
                    //    case 3:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 4:
                    //        EndDayOfMonth = 30;
                    //        break;
                    //    case 5:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 6:
                    //        EndDayOfMonth = 30;
                    //        break;
                    //    case 7:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 8:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 9:
                    //        EndDayOfMonth = 30;
                    //        break;
                    //    case 10:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //    case 11:
                    //        EndDayOfMonth = 30;
                    //        break;
                    //    case 12:
                    //        EndDayOfMonth = 31;
                    //        break;
                    //}
                    //#endregion
                    ////int KhachHangId = int.Parse(dtTemp.Rows[0]["KhachHangId"].ToString());
                    //double TongTienTheoKhachHangId = 0;
                    ////string temp = string.Empty;
                    //DataView dv = new DataView(dtTemp);
                    //DataTable KhachHangTienSanTable = dv.ToTable(true, "KhachHangId","HoTen");
                    //KhachHangTienSanTable.Columns.Add("SoTien", typeof(double));

                    //DateTime dtNgayDauThang = new DateTime(CurrentYear, CurrentMonth, 1);
                    //DateTime dtNgayCuoiThang = new DateTime(CurrentYear, CurrentMonth, EndDayOfMonth);
                    //foreach (DataRow dr in KhachHangTienSanTable.Rows)
                    //{
                    //    dv.RowFilter = "KhachHangId=" + dr["KhachHangId"].ToString();
                    //    DataTable tableFromView = dv.ToTable();
                    //    for (int i = 0; i < tableFromView.Rows.Count; i++)
                    //    {
                    //        //if (tableFromView.Rows[i]["KhachHangId"].ToString() == dr["KhachHangId"].ToString())
                    //        //{
                    //        int countDay = 0;
                    //        #region Dem Ngay
                    //        switch (tableFromView.Rows[i]["TenNgayTrongTuan"].ToString().ToLower())
                    //        {
                    //            case "thứ hai":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Monday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "thứ ba":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Tuesday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "thứ tư":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Wednesday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "thứ năm":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Thursday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "thứ sáu":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Friday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "thứ bảy":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Saturday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //            case "chủ nhật":
                    //                countDay = CountNumberOfWeekDayInDateRange(DayOfWeek.Sunday, dtNgayDauThang, dtNgayCuoiThang);
                    //                break;
                    //        }
                    //        #endregion
                    //        string[] BatDau = tableFromView.Rows[i]["GioBatDau"].ToString().Split(':');
                    //        string[] KetThuc = tableFromView.Rows[i]["GioKetThuc"].ToString().Split(':');
                    //        DateTime dtBatDau = new DateTime(1, 1, 1, int.Parse(BatDau[0]), int.Parse(BatDau[1]), int.Parse(BatDau[2]));
                    //        DateTime dtKetThuc = new DateTime(1, 1, 1, int.Parse(KetThuc[0]), int.Parse(KetThuc[1]), int.Parse(KetThuc[2]));
                    //        TimeSpan ts = dtKetThuc.Subtract(dtBatDau);
                    //        double TongTien = ts.TotalHours * double.Parse(tableFromView.Rows[i]["GiaTien"].ToString()) * countDay;

                    //        //double TinhTienTheoGio = ts.Hours * double.Parse(tableFromView.Rows[i]["GiaTien"].ToString());
                    //        //double TinhTienTheoPhut = ts.Minutes == 0 ? 0 : (double.Parse(tableFromView.Rows[i]["GiaTien"].ToString()) / (ts.Minutes / 15));
                    //        //double TongTien = (TinhTienTheoGio + TinhTienTheoPhut) * countDay;
                    //        TongTienTheoKhachHangId += TongTien;
                    //        //}
                    //    }
                    //    dr["SoTien"] = TongTienTheoKhachHangId;
                    //    TongTienTheoKhachHangId = 0;
                    //    dv.RowFilter = string.Empty;
                    //}
                    #endregion
                    //DataTable KhachHangTienSanTable = new DataTable();
                    clsCongNoCauLongBUS CongNoBUS = new clsCongNoCauLongBUS();
                    //DataTable dtTemp = CongNoBUS.LayBang();
                    //DataTable dtTemp = CongNoBUS.LayBangCongNoTheoThangNam(CurrentMonth, CurrentYear);
                    //DataView dvKhachHangTienSan = new DataView(dtTemp);
                    //dvKhachHangTienSan.RowFilter = "Thang = " + CurrentMonth.ToString() + " and Nam = " + CurrentYear.ToString();
                    DataTable KhachHangTienSanTable = CongNoBUS.LayBangCongNoTheoThangNam(CurrentMonth, CurrentYear);
                    if (KhachHangTienSanTable.Rows.Count == 0)
                    {
                        MessageBox.Show("Không tìm thấy dữ liệu, vui lòng kiểm tra dữ liệu tại màn hình quản lý công nợ", "Thông báo");
                        return;
                    }
                    #region Tao bang dua vao bao cao
                    DataTable dt1 = new DataTable();
                    DataTable dt2 = new DataTable();
                    dt1.Columns.Add("HoTen", typeof(string));
                    dt1.Columns.Add("SoTien", typeof(double));
                    dt2.Columns.Add("HoTen", typeof(string));
                    dt2.Columns.Add("SoTien", typeof(double));
                    for (int i = 0; i < KhachHangTienSanTable.Rows.Count; i++)
                    {
                        if (i % 2 == 0)
                        {
                            DataRow dr = dt1.NewRow();
                            dr["HoTen"] = KhachHangTienSanTable.Rows[i]["HoTen"].ToString();
                            dr["SoTien"] = double.Parse(KhachHangTienSanTable.Rows[i]["SoTien"].ToString());
                            dt1.Rows.Add(dr);
                        }
                        else
                        {
                            DataRow dr = dt2.NewRow();
                            dr["HoTen"] = KhachHangTienSanTable.Rows[i]["HoTen"].ToString();
                            dr["SoTien"] = double.Parse(KhachHangTienSanTable.Rows[i]["SoTien"].ToString());
                            dt2.Rows.Add(dr);
                        }
                    }
                    #endregion
                    frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
                    if (frm == null)
                    {
                        frm = new frmXemBaoCao();
                        frm.Show();
                    }
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    frm.Activate();
                    frm.uct_ReportViewer.InThongBaoTienSanCauLong(dt1, dt2, CurrentMonth.ToString() + "/" + CurrentYear.ToString());
                }
            }
            catch
            {
                MessageBox.Show("Lỗi xuất thông báo tiền sân cầu lông.", "Thông báo");
            }
        }

        private void mnBieuMau_Click(object sender, EventArgs e)
        {
            frmBieuMau frm = (frmBieuMau)getForm("frmBieuMau");
            if (frm == null)
            {
                frm = new frmBieuMau();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void mnKetToanCauLong_Click(object sender, EventArgs e)
        {
            int CurrentYear = DateTime.Now.Year;
            int CurrentMonth = DateTime.Now.Month;
            int CurrentDay = DateTime.Now.Day;
            #region xac dinh ngay cuoi thang
            int EndDayOfMonth = 0;
            switch (CurrentMonth)
            {
                case 1:
                    EndDayOfMonth = 31;
                    break;
                case 2:
                    EndDayOfMonth = 28;
                    break;
                case 3:
                    EndDayOfMonth = 31;
                    break;
                case 4:
                    EndDayOfMonth = 30;
                    break;
                case 5:
                    EndDayOfMonth = 31;
                    break;
                case 6:
                    EndDayOfMonth = 30;
                    break;
                case 7:
                    EndDayOfMonth = 31;
                    break;
                case 8:
                    EndDayOfMonth = 31;
                    break;
                case 9:
                    EndDayOfMonth = 30;
                    break;
                case 10:
                    EndDayOfMonth = 31;
                    break;
                case 11:
                    EndDayOfMonth = 30;
                    break;
                case 12:
                    EndDayOfMonth = 31;
                    break;
            }
            #endregion
            clsLichChiTietBUS LichChiTietBUS = new clsLichChiTietBUS();
            DataTable dtTemp = LichChiTietBUS.LayBangKetToanCauLong();
            DataView dvBangKetToan = new DataView(dtTemp);
            DataTable dtKhachHangTienSan = dvBangKetToan.ToTable(true, "KhachHangId", "HoTen");
            dtKhachHangTienSan.Columns.Add("STT", typeof(int));
            for (int j = 0; j < dtKhachHangTienSan.Rows.Count; j++)
                dtKhachHangTienSan.Rows[j]["STT"] = j + 1;

            DateTime dtNgayDauThang = new DateTime(CurrentYear, CurrentMonth, 1);
            DateTime dtNgayCuoiThang = new DateTime(CurrentYear, CurrentMonth, EndDayOfMonth);
            DataTable dtBangKetToan = new DataTable();

            dtBangKetToan.Columns.Add("SanCauId", typeof(string));
            dtBangKetToan.Columns.Add("GioBatDau", typeof(string));
            dtBangKetToan.Columns.Add("GioKetThuc", typeof(string));
            dtBangKetToan.Columns.Add("NgayChoi", typeof(string));
            dtBangKetToan.Columns.Add("TongSoNgay", typeof(string));
            dtBangKetToan.Columns.Add("TongGio", typeof(string));
            dtBangKetToan.Columns.Add("ThanhTien", typeof(double));
            dtBangKetToan.Columns.Add("GhiChu", typeof(string));
            dtBangKetToan.Columns.Add("HoTen", typeof(string));
            dtBangKetToan.Columns.Add("KhachHangId", typeof(string));

            foreach (DataRow dr in dtKhachHangTienSan.Rows)
            {
                string hoten = dr["HoTen"].ToString();
                string strNgayChoiCauTrongTuan = string.Empty;
                string strNgayChoiCauCuoiTuan = string.Empty;
                int SoNgayTrongTuan = 0;
                int SoNgayCuoiTuan = 0;
                double SoGioTrongTuan = 0;
                double SoGioCuoiTuan = 0;

                dvBangKetToan.RowFilter = "KhachHangId=" + dr["KhachHangId"].ToString();
                DataTable dtLichSanCauTheoKhachHangID = dvBangKetToan.ToTable();
                DataView dvLichSanTrongTuan = new DataView(dtLichSanCauTheoKhachHangID);
                DataView dvLichSanCuoiTuan = new DataView(dtLichSanCauTheoKhachHangID);

                dvLichSanTrongTuan.RowFilter = "NgayTrongTuanId < 7";
                DataTable dtGioBDGioKTCuaMotKhachHangTrongTuan = dvLichSanTrongTuan.ToTable(true, "KhachHangId", "HoTen", "SanCauId", "GioBatDau", "GioKetThuc", "GiaTien");
                dtGioBDGioKTCuaMotKhachHangTrongTuan.Columns.Add("LietKeNgay", typeof(string));
                dtGioBDGioKTCuaMotKhachHangTrongTuan.Columns.Add("TongNgay", typeof(int));
                dtGioBDGioKTCuaMotKhachHangTrongTuan.Columns.Add("TongGio", typeof(double));
                dtGioBDGioKTCuaMotKhachHangTrongTuan.Columns.Add("ThanhTienTheoGio", typeof(double));
                foreach (DataRow drow in dtGioBDGioKTCuaMotKhachHangTrongTuan.Rows)
                {
                    drow["TongNgay"] = 0;
                    drow["TongGio"] = 0;
                }
                dvLichSanCuoiTuan.RowFilter = "NgayTrongTuanId >= 7";
                DataTable dtGioBDGioKTCuaMotKhachHangCuoiTuan = dvLichSanCuoiTuan.ToTable(true, "KhachHangId", "HoTen", "SanCauId", "GioBatDau", "GioKetThuc", "GiaTien");
                dtGioBDGioKTCuaMotKhachHangCuoiTuan.Columns.Add("LietKeNgay", typeof(string));
                dtGioBDGioKTCuaMotKhachHangCuoiTuan.Columns.Add("TongNgay", typeof(int));
                dtGioBDGioKTCuaMotKhachHangCuoiTuan.Columns.Add("TongGio", typeof(double));
                dtGioBDGioKTCuaMotKhachHangCuoiTuan.Columns.Add("ThanhTienTheoGio", typeof(double));
                foreach (DataRow drow in dtGioBDGioKTCuaMotKhachHangCuoiTuan.Rows)
                {
                    drow["TongNgay"] = 0;
                    drow["TongGio"] = 0;
                }

                for (int i = 0; i < dtLichSanCauTheoKhachHangID.Rows.Count; i++)
                {
                    bool flagTrongTuan = false;
                    bool flagCuoiTuan = false;

                    string[] BatDau = dtLichSanCauTheoKhachHangID.Rows[i]["GioBatDau"].ToString().Split(':');
                    string[] KetThuc = dtLichSanCauTheoKhachHangID.Rows[i]["GioKetThuc"].ToString().Split(':');
                    DateTime dtBatDau = new DateTime(1, 1, 1, int.Parse(BatDau[0]), int.Parse(BatDau[1]), int.Parse(BatDau[2]));
                    DateTime dtKetThuc = new DateTime(1, 1, 1, int.Parse(KetThuc[0]), int.Parse(KetThuc[1]), int.Parse(KetThuc[2]));
                    TimeSpan ts = dtKetThuc.Subtract(dtBatDau);
                    #region Dem Ngay
                    switch (dtLichSanCauTheoKhachHangID.Rows[i]["TenNgayTrongTuan"].ToString().ToLower())
                    {
                        case "thứ hai":
                            SoNgayTrongTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Monday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioTrongTuan = (ts.TotalHours * SoNgayTrongTuan);
                            strNgayChoiCauTrongTuan = "H";
                            flagTrongTuan = true;
                            break;
                        case "thứ ba":
                            SoNgayTrongTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Tuesday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioTrongTuan = (ts.TotalHours * SoNgayTrongTuan);
                            strNgayChoiCauTrongTuan = "Ba";
                            flagTrongTuan = true;
                            break;
                        case "thứ tư":
                            SoNgayTrongTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Wednesday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioTrongTuan = (ts.TotalHours * SoNgayTrongTuan);
                            strNgayChoiCauTrongTuan = "T";
                            flagTrongTuan = true;
                            break;
                        case "thứ năm":
                            SoNgayTrongTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Thursday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioTrongTuan = (ts.TotalHours * SoNgayTrongTuan);
                            strNgayChoiCauTrongTuan = "N";
                            flagTrongTuan = true;
                            break;
                        case "thứ sáu":
                            SoNgayTrongTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Friday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioTrongTuan = (ts.TotalHours * SoNgayTrongTuan);
                            strNgayChoiCauTrongTuan = "S";
                            flagTrongTuan = true;
                            break;
                        case "thứ bảy":
                            SoNgayCuoiTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Saturday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioCuoiTuan = (ts.TotalHours * SoNgayCuoiTuan);
                            strNgayChoiCauCuoiTuan = "B";
                            flagCuoiTuan = true;
                            break;
                        case "chủ nhật":
                            SoNgayCuoiTuan = CountNumberOfWeekDayInDateRange(DayOfWeek.Sunday, dtNgayDauThang, dtNgayCuoiThang);
                            SoGioCuoiTuan = (ts.TotalHours * SoNgayCuoiTuan);
                            strNgayChoiCauCuoiTuan = "Cn";
                            flagCuoiTuan = true;
                            break;
                    }
                    #endregion
                    if (flagTrongTuan)
                    {
                        foreach (DataRow dtrow in dtGioBDGioKTCuaMotKhachHangTrongTuan.Rows)
                        {
                            if (dtrow["SanCauId"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["SanCauId"].ToString()
                            && dtrow["GioBatDau"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["GioBatDau"].ToString()
                            && dtrow["GioKetThuc"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["GioKetThuc"].ToString())
                            {
                                dtrow["TongNgay"] = int.Parse(dtrow["TongNgay"].ToString()) + SoNgayTrongTuan;
                                dtrow["TongGio"] = double.Parse(dtrow["TongGio"].ToString()) + SoGioTrongTuan;
                                dtrow["LietKeNgay"] = dtrow["LietKeNgay"].ToString() + "-" + strNgayChoiCauTrongTuan;
                            }
                        }
                    }
                    if (flagCuoiTuan)
                    {
                        foreach (DataRow dtrow in dtGioBDGioKTCuaMotKhachHangCuoiTuan.Rows)
                        {
                            if (dtrow["SanCauId"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["SanCauId"].ToString()
                            && dtrow["GioBatDau"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["GioBatDau"].ToString()
                            && dtrow["GioKetThuc"].ToString() == dtLichSanCauTheoKhachHangID.Rows[i]["GioKetThuc"].ToString())
                            {
                                dtrow["TongNgay"] = int.Parse(dtrow["TongNgay"].ToString()) + SoNgayCuoiTuan;
                                dtrow["TongGio"] = double.Parse(dtrow["TongGio"].ToString()) + SoGioCuoiTuan;
                                dtrow["LietKeNgay"] = dtrow["LietKeNgay"].ToString() + "-" + strNgayChoiCauCuoiTuan;
                            }
                        }
                    }
                }
                if (SoNgayTrongTuan > 0)
                {
                    foreach (DataRow drow in dtGioBDGioKTCuaMotKhachHangTrongTuan.Rows)
                    {
                        DataRow drBangKetToan = dtBangKetToan.NewRow();
                        drBangKetToan["SanCauId"] = drow["SanCauId"].ToString();
                        drBangKetToan["GioBatDau"] = drow["GioBatDau"].ToString();
                        drBangKetToan["GioKetThuc"] = drow["GioKetThuc"].ToString();
                        drBangKetToan["NgayChoi"] = drow["LietKeNgay"].ToString();
                        drBangKetToan["TongSoNgay"] = drow["TongNgay"].ToString();
                        drBangKetToan["TongGio"] = drow["TongGio"].ToString();
                        drBangKetToan["ThanhTien"] = double.Parse(drow["TongGio"].ToString()) * double.Parse(drow["GiaTien"].ToString());
                        drBangKetToan["GhiChu"] = "";
                        drBangKetToan["HoTen"] = drow["HoTen"].ToString();
                        drBangKetToan["KhachHangId"] = drow["KhachHangId"].ToString();
                        dtBangKetToan.Rows.Add(drBangKetToan);
                    }
                }
                if (SoNgayCuoiTuan > 0)
                {
                    foreach (DataRow drow in dtGioBDGioKTCuaMotKhachHangCuoiTuan.Rows)
                    {
                        DataRow drBangKetToan = dtBangKetToan.NewRow();
                        drBangKetToan["KhachHangId"] = drow["KhachHangId"].ToString();
                        drBangKetToan["HoTen"] = drow["HoTen"].ToString();
                        drBangKetToan["SanCauId"] = drow["SanCauId"].ToString();
                        drBangKetToan["GioBatDau"] = drow["GioBatDau"].ToString();
                        drBangKetToan["GioKetThuc"] = drow["GioKetThuc"].ToString();
                        drBangKetToan["NgayChoi"] = drow["LietKeNgay"].ToString();
                        drBangKetToan["TongSoNgay"] = drow["TongNgay"].ToString();
                        drBangKetToan["TongGio"] = drow["TongGio"].ToString();
                        drBangKetToan["ThanhTien"] = double.Parse(drow["TongGio"].ToString()) * double.Parse(drow["GiaTien"].ToString());
                        drBangKetToan["GhiChu"] = "";
                        drBangKetToan["HoTen"] = drow["HoTen"].ToString();
                        drBangKetToan["KhachHangId"] = drow["KhachHangId"].ToString();
                        dtBangKetToan.Rows.Add(drBangKetToan);
                    }
                }
                dvBangKetToan.RowFilter = string.Empty;
            }
            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBangKetToanCauLong(dtBangKetToan, CurrentMonth.ToString() + "/" + CurrentYear.ToString());
        }

        private void ShowTimKiemPhieuThuPhieuChi()
        {
            frmTimKiemPhieuGiaoDich frm = (frmTimKiemPhieuGiaoDich)getForm("frmTimKiemPhieuGiaoDich");
            if (frm == null)
            {
                frm = new frmTimKiemPhieuGiaoDich();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }


        private void mnTimKiemPhieuThuPhieuChi_Click(object sender, EventArgs e)
        {
            ShowTimKiemPhieuThuPhieuChi();
        }

        private void mnPhieuThu_Click(object sender, EventArgs e)
        {
            ShowXuatPhieuThu();
        }

        private void mnPhieuChi_Click(object sender, EventArgs e)
        {
            ShowXuatPhieuChi();
        }

        private void mnBaoCaoDoanhThu_Click(object sender, EventArgs e)
        {
            ShowBaoCaoBanHang();
            
        }


        private void ShowThongKeDoanhThuChiPhi()
        {
            frmThongKeDoanhThuChiPhi frm = (frmThongKeDoanhThuChiPhi)getForm("frmThongKeDoanhThuChiPhi");
            if (frm == null)
            {
                frm = new frmThongKeDoanhThuChiPhi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void ShowThongKeKhachHang()
        {
            frmThongKeKhachHangTangTruong frm = (frmThongKeKhachHangTangTruong)getForm("frmThongKeDoanhThu");
            if (frm == null)
            {
                frm = new frmThongKeKhachHangTangTruong();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnThongKeKhachHangTangTruong_Click(object sender, EventArgs e)
        {
            ShowThongKeKhachHang();
        }

        private void mnThongKeDoanhThuChiPhi_Click(object sender, EventArgs e)
        {
            ShowThongKeDoanhThuChiPhi();
        }

        private void mnBaoCaoChiPhi_Click(object sender, EventArgs e)
        {
            ShowBaoCaoBanHang();
        }

        private void mnThayDoiMatKhau_Click(object sender, EventArgs e)
        {
            ShowDoiMatKhau();
        }

        private void ShowDoiMatKhau()
        {
            frmDoiMatKhau frm = (frmDoiMatKhau)getForm("frmDoiMatKhau");
            if (frm == null)
            {
                frm = new frmDoiMatKhau();
                frm.ShowDialog();
            }
            frm.MdiParent = this;
           //frm.WindowState = FormWindowState.Maximized;
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnQuanLyDiaChi_Click(object sender, EventArgs e)
        {
            ShowQuanLyDiaChi();
        }

        private void ShowQuanLyDiaChi()
        {
            frmDanhMucDiaChi frm = (frmDanhMucDiaChi)getForm("frmDanhMucDiaChi");
            if (frm == null)
            {
                frm = new frmDanhMucDiaChi();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnXemLichSaoLuu_Click(object sender, EventArgs e)
        {
            frmLichSanCauSaoLuu frm = (frmLichSanCauSaoLuu)getForm("frmLichSanCauSaoLuu");
            if (frm == null)
            {
                frm = new frmLichSanCauSaoLuu();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void mnBaoCaoSoLuongKhachHang_Click(object sender, EventArgs e)
        {
            clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
            DataTable dt = KhachHangBUS.LayBang();
            DataView dv = new DataView(dt);
            dv.RowFilter = "MaKhachHang like 'TH*'";
            int CurrentMonth = DateTime.Now.Month;
            int CurrentYear = DateTime.Now.Year;
            string NgayIn = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + CurrentMonth.ToString() + " năm " + CurrentYear.ToString();
            DataTable dtKq = dv.ToTable();
            dtKq.Columns.Add("STT", typeof(string));
            for (int i = 0; i < dtKq.Rows.Count; i++)
            {
                dtKq.Rows[i]["STT"] = (i + 1).ToString();
            }
            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBaoCaoSoLuongKhachHang(dtKq, CurrentMonth.ToString() + "/" + CurrentYear.ToString(), NgayIn);
        }

        private void mnQuanLyTheHocVien_Click(object sender, EventArgs e)
        {
            
        }

        private void mnBaoCaoSoLuongKhachHangKhuyenMai_Click(object sender, EventArgs e)
        {
            clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
            clsKhuyenMaiBUS KhuyenMaiBUS = new clsKhuyenMaiBUS();
            DataTable dtKhuyenMai = KhuyenMaiBUS.LayBang();
            string ChuThich = string.Empty;
            foreach (DataRow dr in dtKhuyenMai.Rows)
                ChuThich += dr["KhuyenMaiId"].ToString() + " : " + dr["TenKhuyenMai"].ToString() + "\n";

            DataTable dtKhachHangVaThe = KhachHangBUS.LayBangKhachHangVaThe();
            DataView dv = new DataView(dtKhachHangVaThe);
            dv.RowFilter = "KhuyenMaiId > 0";
            DataTable dtKq = dv.ToTable(true, "MaKhachHang", "HoTen", "NgaySinh", "SoDienThoai", "DiaChi", "KhuyenMaiId");
            dtKq.Columns.Add("STT", typeof(string));
            for (int i = 0; i < dtKq.Rows.Count; i++)
            {
                dtKq.Rows[i]["STT"] = (i + 1).ToString();
            }
            int CurrentMonth = DateTime.Now.Month;
            int CurrentYear = DateTime.Now.Year;
            string NgayIn = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + CurrentMonth.ToString() + " năm " + CurrentYear.ToString();

            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBaoCaoSoLuongKhachHangHuongKhuyenMai(dtKq, CurrentMonth.ToString() + "/" + CurrentYear.ToString(), NgayIn, ChuThich);
        
        }

        private void mnBaoCaoTheHetHan_Click(object sender, EventArgs e)
        {
            clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
            DataTable dtKhachHangVaThe = KhachHangBUS.LayBangKhachHangVaThe();
            DataView dv = new DataView(dtKhachHangVaThe);
            
            DateTime NgayXetDenHan = DateTime.Now.Add(new TimeSpan(7,0,0,0));
            dv.RowFilter = "NgayKetThuc <= #" + NgayXetDenHan.ToString() + "# and NgayKetThuc > #" + DateTime.Now + "#";
            DataTable dtKq = dv.ToTable(true, "MaKhachHang", "HoTen", "MaThe", "NgayBatDau", "NgayKetThuc");
            dtKq.Columns.Add("STT", typeof(string));
            for (int i = 0; i < dtKq.Rows.Count; i++)
                dtKq.Rows[i]["STT"] = (i + 1).ToString();
            int CurrentMonth = DateTime.Now.Month;
            int CurrentYear = DateTime.Now.Year;
            string NgayIn = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + CurrentMonth.ToString() + " năm " + CurrentYear.ToString();

            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBaoCaoSoLuongTheSapHetHan(dtKq, CurrentMonth.ToString() + "/" + CurrentYear.ToString(), NgayIn);
        }

        private void mnBaoCaoSoLuongTheDaCapTheoNguoiDung_Click(object sender, EventArgs e)
        {
            clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
            DataTable dtKhachHangVaThe = KhachHangBUS.LayBangKhachHangVaThe();
            DataView dv = new DataView(dtKhachHangVaThe);
            dv.Sort = "KhachHangId";
            DataTable dtKq = dv.ToTable(true, "MaKhachHang", "HoTen", "MaThe", "NgayBatDau", "NgayKetThuc");
            dtKq.Columns.Add("STT", typeof(string));
            for (int i = 0; i < dtKq.Rows.Count; i++)
                dtKq.Rows[i]["STT"] = (i + 1).ToString();
            
            int CurrentMonth = DateTime.Now.Month;
            int CurrentYear = DateTime.Now.Year;
            string NgayIn = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + CurrentMonth.ToString() + " năm " + CurrentYear.ToString();

            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBaoCaoSoLuongTheDaCapTheoKhachHang(dtKq, CurrentMonth.ToString() + "/" + CurrentYear.ToString(), NgayIn);
        }

        private void mnLienHe_Click(object sender, EventArgs e)
        {
            frmLienHe frm = (frmLienHe)getForm("frmLienHe");
            if (frm == null)
            {
                frm = new frmLienHe();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
        }

        private void mnBaoCaoSoLuongTheDaCap_Click(object sender, EventArgs e)
        {
            clsKhachHangBUS KhachHangBUS = new clsKhachHangBUS();
            DataTable dtKhachHangVaThe = KhachHangBUS.LayBangKhachHangVaThe();
            DataView dv = new DataView(dtKhachHangVaThe);
            dv.Sort = "KhachHangId";
            DataTable dtKq = dv.ToTable(true, "KhachHangId", "MaKhachHang", "HoTen");
            dtKq.Columns.Add("STT", typeof(string));
            dtKq.Columns.Add("SoThe", typeof(string));
            for (int i = 0; i < dtKq.Rows.Count; i++)
            {
                dtKq.Rows[i]["STT"] = (i + 1).ToString();
                dv.RowFilter = string.Empty;
                dv.RowFilter = "KhachHangId = " + dtKq.Rows[i]["KhachHangId"].ToString();
                dtKq.Rows[i]["SoThe"] = dv.Count.ToString();
            }

            int CurrentMonth = DateTime.Now.Month;
            int CurrentYear = DateTime.Now.Year;
            string NgayIn = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + CurrentMonth.ToString() + " năm " + CurrentYear.ToString();

            frmXemBaoCao frm = (frmXemBaoCao)getForm("frmXemBaoCao");
            if (frm == null)
            {
                frm = new frmXemBaoCao();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Activate();
            frm.uct_ReportViewer.InBaoCaoSoLuongTheDaCap(dtKq, CurrentMonth.ToString() + "/" + CurrentYear.ToString(), NgayIn);
        
        }

        private void mnBangGiaCauLongVangLai_Click(object sender, EventArgs e)
        {
            frmBangGiaCauLongVangLai frm = (frmBangGiaCauLongVangLai)getForm("frmBangGiaCauLongVangLai");
            if (frm == null)
            {
                frm = new frmBangGiaCauLongVangLai();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnQuanLyCongNo_Click(object sender, EventArgs e)
        {
            frmQuanLyCongNo frm = (frmQuanLyCongNo)getForm("frmQuanLyCongNo");
            if (frm == null)
            {
                frm = new frmQuanLyCongNo();
                frm.Show();
            }
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            //frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Activate();
        }

        private void mnBaoCaoBanHangHangNgay_Click(object sender, EventArgs e)
        {
            ShowBaoCaoBanHangHangNgay();
        }

        private void mnLogout_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }

        private void mnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }


}